package com.itwxz.system.controller.mobile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwxz.common.core.domain.R;
import com.itwxz.common.utils.SecurityUtils;
import com.itwxz.common.utils.StringUtils;
import com.itwxz.system.domain.dto.CreateWxzOrderDto;
import com.itwxz.system.domain.po.WxzOrder;
import com.itwxz.system.domain.vo.OperateOrderListVo;
import com.itwxz.system.domain.vo.SearchOrderVo;
import com.itwxz.system.domain.vo.StaffRankingVo;
import com.itwxz.system.service.IWxzOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 维修站订单Controller
 *
 * @author gastronome
 * @date 2025-04-05
 */
@Tag(name = "维修站手机端订单Controller")
@Slf4j
@RestController
@RequestMapping("/mobile/order")
@RequiredArgsConstructor
public class WxzOrderMobileController {
    private final IWxzOrderService orderService;

    @PostMapping
    @Operation(summary = "学生报修订单接口", description = "报修订单信息")
    @Parameter(name = "order", description = "报修订单表")
    public R<String> createWxzOrder(@RequestBody CreateWxzOrderDto order) {
        return orderService.createWxzOrder(order);
    }

    @PutMapping("/accept/{orderId}")
    @Operation(summary = "接单接口", description = "接单并修改订单状态为已接单")
    @Parameter(name = "orderId", description = "订单ID")
    public R<String> acceptWxzOrder(@PathVariable("orderId") String orderId) {
        return orderService.accepAndUpdateOrder(orderId, SecurityUtils.getUserId());
    }

    @PutMapping("/transfer/{orderId}")
    @Operation(summary = "转让订单接口", description = "将订单转让给其他维修人员")
    @Parameter(name = "orderId", description = "订单ID")
    public R<String> transferWxzOrder(@PathVariable("orderId") Long orderId) {
        return orderService.transferOrder(orderId, SecurityUtils.getUserId());
    }

    @PutMapping("/complete/{orderId}")
    @Operation(summary = "完成订单接口", description = "将订单状态修改为已完成")
    @Parameter(name = "orderId", description = "订单ID")
    public R<String> completeWxzOrder(@PathVariable("orderId") Long orderId) {
        return orderService.completeOrderById(orderId, SecurityUtils.getUserId());
    }

    @GetMapping("/list")
    @Operation(summary = "查看待接订单列表接口", description = "查看待接订单列表")
    public R<OperateOrderListVo> getPendingList() {
        return R.ok(orderService.getOperateList());
    }

    @GetMapping("/list/me")
    @Operation(summary = "查看我的历史订单列表接口", description = "查看我的历史订单订单列表")
    public R<Page<WxzOrder>> getListMe(int pageNum, int pageSize, String query) {
        Page<WxzOrder> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<WxzOrder> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(WxzOrder::getStaffId, SecurityUtils.getUserId());

        if (StringUtils.isNotBlank(query)) {
            wrapper.and(w -> w.like(WxzOrder::getName, query)
                    .or()
                    .like(WxzOrder::getDetails, query));
        }

        wrapper.orderByDesc(WxzOrder::getCreateTime);


        orderService.page(page, wrapper);
        return R.ok(page);
    }

    @GetMapping("/in-progress/me")
    @Operation(
            summary = "获取当前用户的进行中订单",
            description = "此接口用于获取当前登录用户的进行中的订单"
    )
    public R<List<WxzOrder>> getInProgressListByMe() {
        return R.ok(orderService.getInProgressListByMe(SecurityUtils.getUserId()));
    }

    @GetMapping("/search")
    @Operation(
            summary = "根据学号、姓名或手机号搜索订单",
            description = "通过学号、姓名或手机号查询相关订单"
    )
    @Parameter(name = "keyword", description = "搜索关键词（学号、姓名或手机号）")
    public R<List<SearchOrderVo>> searchOrder(@RequestParam String keyword) {
        return R.ok(orderService.searchOrder(keyword));
    }

    // $2a$10$kuMba.Cvx0P4IJ7Tf8qudu89MpMeRLjfqzsfFHYpKP5cuxSD7w0li
    @GetMapping("/ranking")
    public R<List<StaffRankingVo>> getRanking() {
        return R.ok(orderService.getRanking());
    }
}
